﻿namespace Minesweeper
{
    using System;
    using System.Text;

    class MineFieldOldVersion
    {
        private const char block = (char)0x2588;

        public class Cell
        {
            public const int CellHeight = 5;
            public const int CellWidth = 3;

            public static readonly string[] NotOpnenedBlock =
            {
                string.Format("{0}{0}{0}", block),
                string.Format("{0}{0}{0}", block), 
                string.Format("{0}{0}{0}", block), 
                string.Format("{0}{0}{0}", block), 
                string.Format("{0}{0}{0}", block),
            };

            public static readonly string[] Mine =
            {
               "...",
               "\\|/",
               "-@-",
               "/|\\",
               "...",
            };

            public static readonly string[] Flag =
            {
                "..#",
                ".##",
                "###",
                "..#",
                "###",
            };

            public static readonly string[] ZeroBlock =
            {
                "...",
                "...",
                "...",
                "...",
                "...",
            };

            public static readonly string[] OneBlock =
            {
                ".#.",
                "##.",
                ".#.",
                ".#.",
                "###",
            };

            public static readonly string[] TwoBlock =
            {
                "###",
                "..#",
                "###",
                "#..",
                "###",
            };

            public static readonly string[] ThreeBlock =
            {
                "###",
                "..#",
                "###",
                "..#",
                "###",
            };

            public static readonly string[] FourBlock =
            {
                ".##",
                "#.#",
                "###",
                "..#",
                "..#",
            };

            public static readonly string[] FiveBlock =
            {
                "###",
                "#..",
                "###",
                "..#",
                "###",
            };

            public static readonly string[] SixBlock =
            {
                "###",
                "#.. ",
                "### ",
                "#.# ",
                "### ",
            };

            public static readonly string[] SevenBlock =
            {
                "###",
                "..#",
                ".# ",
                ".#.",
                ".#.",
            };

            public static readonly string[] EightBlock =
            {
                "###",
                "#.#",
                "###",
                "#.#",
                "###",
            };

            public static void Draw(int x, int y, string[] block, ConsoleColor color = ConsoleColor.Black, bool isMineOrFlag = false)
            {
                int currentY = y;
                Console.BackgroundColor = color;
                if (isMineOrFlag == true) Console.ForegroundColor = ConsoleColor.Red;
                Console.Write(new string(' ', 5));
                Console.SetCursorPosition(x, ++currentY);

                for (int blockRow = 0; blockRow < block.Length; blockRow++)
                {
                    Console.BackgroundColor = color;
                    Console.Write(" ");
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.Write(block[blockRow]);
                    Console.BackgroundColor = color;
                    Console.Write(" ");
                    Console.SetCursorPosition(x, ++currentY);
                }

                Console.BackgroundColor = color;
                Console.Write(new string(' ', 5));
                Console.ForegroundColor = ConsoleColor.Gray;
            }

            public static void DrawOnlyFrame(int x, int y, ConsoleColor color = ConsoleColor.Black)
            {
                int currentY = y;
                Console.BackgroundColor = color;
                Console.Write(new string(' ', 5));
                Console.SetCursorPosition(x, ++currentY);

                for (int blockRow = 0; blockRow < CellHeight; blockRow++)
                {
                    Console.Write(" ");
                    Console.SetCursorPosition(x + CellWidth + 1, currentY);
                    Console.Write(" ");
                    Console.SetCursorPosition(x, ++currentY);
                }

                Console.BackgroundColor = color;
                Console.Write(new string(' ', 5));
            }
        }

        private int height;
        private int width;
        private int numberOfMines;
        private int currentRow;
        private int currentCol;
        private int[,] field; //each cell has value == -1 if there is a mine or value >= 0 indicating the number of mines next to it 
        private bool[,] visited;

        public MineFieldOldVersion(int height, int width, int numberOfMines)
        {
            this.height = height;
            this.width = width;
            this.field = new int[this.height, this.width];
            this.visited = new bool[this.height, this.width];
            this.numberOfMines = numberOfMines;
            this.currentRow = 0;
            this.currentCol = 0;
        }

        #region Properties
        public int Width
        {
            get
            {
                return this.width;
            }
        }

        public int Height
        {
            get
            {
                return this.height;
            }
        }

        public int NumberOfMines
        {
            get
            {
                return this.numberOfMines;
            }
        }

        public int CurrentRow
        {
            get
            {
                return this.currentRow;
            }
            set
            {
                this.currentRow = value;
            }
        }

        public int CurrentCol
        {
            get
            {
                return this.currentCol;
            }
            set
            {
                this.currentCol = value;
            }
        }

        public int this[int row, int col]
        {
            get
            {
                return this.field[row, col];
            }
            set
            {
                this.field[row, col] = value;
            }
        }
        #endregion

        public void Visit(int row, int col)
        {
            this.visited[row, col] = true;
        }

        public bool IsVisited(int row, int col)
        {
            return this.visited[row, col];
        }

        public bool HasMine(int row, int col)
        {
            return this.field[row, col] == -1;
        }

        public void InitialDraw()
        {
            Console.WriteLine();
            for (int row = 1; row < this.Height * 5 + 1; row++)
            {
                Console.Write(" ");
                for (int col = 0; col < this.Width; col++)
                {
                    Console.Write("{0}{0}{0} ", block);
                }
                Console.WriteLine();
                if (row % 5 == 0)
                {
                    Console.WriteLine();
                }
            }
        }

        public void PlaceMines()
        {
        }
    }
}

/*namespace Minesweeper
{
    using System;
    using System.Threading;

    public class MinesweeperGame
    {
        static MineFieldOldVersion playField = new MineFieldOldVersion(4, 5, 0);
        static int rightBoarder;
        static int bottomBoarder;

        private static void SetConsoleSizeAndBorders()
        {
            Console.BufferHeight = Console.WindowHeight = 30;
            Console.BufferWidth = Console.WindowWidth = 140;
            rightBoarder = playField.Width * 3 + playField.Width + 1;
            bottomBoarder = playField.Height * 5 + playField.Height + 1;
        }

        static void HighlightCell(int currentX, int currentY)
        {
            Console.SetCursorPosition(currentX, currentY);
            MineFieldOldVersion.Cell.DrawOnlyFrame(currentX, currentY, ConsoleColor.Green);
            Console.SetCursorPosition(currentX, currentY);
        }

        static void UnHighlightCell(int currentX, int currentY)
        {
            Console.SetCursorPosition(currentX, currentY);
            MineFieldOldVersion.Cell.DrawOnlyFrame(currentX, currentY, ConsoleColor.Black);
            Console.SetCursorPosition(currentX, currentY);
        }

        static void DrawOpenedBlock(int currentX, int currentY, ConsoleColor color)
        {
            if (playField[playField.CurrentRow, playField.CurrentCol] == 0)
            {
                MineFieldOldVersion.Cell.Draw(currentX, currentY, MineFieldOldVersion.Cell.ZeroBlock, color);
            }
            else if (playField[playField.CurrentRow, playField.CurrentCol] == -1)
            {
                MineFieldOldVersion.Cell.Draw(currentX, currentY, MineFieldOldVersion.Cell.Mine, ConsoleColor.Red, true);
                Environment.Exit(0);
            }
            else if (playField[playField.CurrentRow, playField.CurrentCol] == 1)
            {
                MineFieldOldVersion.Cell.Draw(currentX, currentY, MineFieldOldVersion.Cell.OneBlock, color);
            }
            else if (playField[playField.CurrentRow, playField.CurrentCol] == 2)
            {
                MineFieldOldVersion.Cell.Draw(currentX, currentY, MineFieldOldVersion.Cell.TwoBlock, color);
            }
            else if (playField[playField.CurrentRow, playField.CurrentCol] == 3)
            {
                MineFieldOldVersion.Cell.Draw(currentX, currentY, MineFieldOldVersion.Cell.ThreeBlock, color);
            }
            else if (playField[playField.CurrentRow, playField.CurrentCol] == 4)
            {
                MineFieldOldVersion.Cell.Draw(currentX, currentY, MineFieldOldVersion.Cell.FourBlock, color);
            }
            else if (playField[playField.CurrentRow, playField.CurrentCol] == 5)
            {
                MineFieldOldVersion.Cell.Draw(currentX, currentY, MineFieldOldVersion.Cell.FiveBlock, color);
            }
            else if (playField[playField.CurrentRow, playField.CurrentCol] == 6)
            {
                MineFieldOldVersion.Cell.Draw(currentX, currentY, MineFieldOldVersion.Cell.SixBlock, color);
            }
            else if (playField[playField.CurrentRow, playField.CurrentCol] == 7)
            {
                MineFieldOldVersion.Cell.Draw(currentX, currentY, MineFieldOldVersion.Cell.SevenBlock, color);
            }
            else if (playField[playField.CurrentRow, playField.CurrentCol] == 8)
            {
                MineFieldOldVersion.Cell.Draw(currentX, currentY, MineFieldOldVersion.Cell.EightBlock, color);
            }
        }

        static void OpenCell(int currentX, int currentY)
        {
            Console.SetCursorPosition(currentX, currentY);
            DrawOpenedBlock(currentX, currentY, ConsoleColor.Green);
            playField.Visit(playField.CurrentRow, playField.CurrentCol);
            Console.SetCursorPosition(currentX, currentY);
        }

        static void PlaceFlag(int currentX, int currentY)
        {
            Console.SetCursorPosition(currentX, currentY);
            if (!playField.IsVisited(playField.CurrentRow, playField.CurrentCol))
            {
                MineFieldOldVersion.Cell.Draw(currentX, currentY, MineFieldOldVersion.Cell.Flag, ConsoleColor.Green, true);
            }
            Console.SetCursorPosition(currentX, currentY);
        }

        private static void Instructions()
        {

        }

        private static void SelectMenu()
        {
            ConsoleSettings();
            int userChoice = 0;
            while (true)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(@"                _              _     __  __  _                   
               | |            | |   |  \/  |(_)                  
               | | _   _  ___ | |_  | \  / | _  _ __    ___  ___ 
           _   | || | | |/ __|| __| | |\/| || || '_ \  / _ \/ __|
          | |__| || |_| |\__ \| |_  | |  | || || | | ||  __/\__ \
           \____/  \__,_||___/ \__| |_|  |_||_||_| |_| \___||___/
                                                        
                                                        ");
                Console.ForegroundColor = ConsoleColor.Cyan;
                string spaces = new string(' ', 30);
                Console.WriteLine("{0} New game", spaces);
                Console.WriteLine("{0} Instructions", spaces);
                Console.WriteLine("{0} Highscores", spaces);
                Console.WriteLine("{0} Exit", spaces);
                Console.WriteLine("\n\n\n");
                string separator = new string('-', 30);
                Console.ForegroundColor = ConsoleColor.DarkMagenta;
                Console.WriteLine(@"            ______________________________       . \  | / . 
           /                            / \        \ \ / /
           |       Team Dazzler         |{========= >- -<
           \____________________________\_/        / / \ \
                                                 . /  | \ .");
                Console.CursorVisible = false;
                Console.SetCursorPosition(28, 8 + userChoice);
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("->");
                ConsoleKeyInfo key = new ConsoleKeyInfo();
                key = Console.ReadKey(true);
                if (key.Key == ConsoleKey.DownArrow)
                {
                    userChoice++;
                    if (userChoice > 3)
                    {
                        userChoice %= 4;
                    }
                }
                if (key.Key == ConsoleKey.UpArrow)
                {
                    userChoice--;
                    if (userChoice < 0)
                    {
                        userChoice = 3;
                    }
                }
                if (key.Key == ConsoleKey.Enter)
                {
                    break;
                }
                Console.Clear();
            }
            switch (userChoice)
            {
                case 0:
                    //Enter name->Difficulty->Game
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case 1:
                    //Import file with controls and instructions
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case 2:
                    //show the highscore table
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
                case 3:
                    //Just exit the game
                    Console.Clear();
                    Console.ForegroundColor = ConsoleColor.Gray;
                    break;
            }

        }

        private static void ConsoleSettings()
        {
            Console.CursorVisible = false;
            Console.BufferHeight = Console.WindowHeight;
            Console.BufferWidth = Console.WindowWidth;
            Console.WindowHeight = 25;
            Console.WindowWidth = 80;
            Console.Title = "JustMines - Team Dazzler";
            Thread.Sleep(120);
        }

        public static void Main()
        {
            SelectMenu();
            SetConsoleSizeAndBorders();

            playField.InitialDraw();
            playField[0, 0] = 2;
            playField[0, 1] = 8;
            playField[1, 1] = 3;
            playField[1, 0] = -1;

            int currentCursorX = 0;
            int currentCursorY = 0;
            Console.SetCursorPosition(currentCursorX, currentCursorY);

            while (true)
            {
                ConsoleKeyInfo pressedKey = Console.ReadKey(true); //Waiting to press key <- intended

                while (Console.KeyAvailable) Console.ReadKey(true);

                if (pressedKey.Key == ConsoleKey.LeftArrow)
                {
                    UnHighlightCell(currentCursorX, currentCursorY);
                    if (currentCursorX - (MineFieldOldVersion.Cell.CellWidth + 1) >= 0)
                    {
                        currentCursorX -= (MineFieldOldVersion.Cell.CellWidth + 1);
                        playField.CurrentCol--;
                    }
                    HighlightCell(currentCursorX, currentCursorY);
                }
                else if (pressedKey.Key == ConsoleKey.RightArrow)
                {
                    UnHighlightCell(currentCursorX, currentCursorY);
                    if (currentCursorX + 2 * (MineFieldOldVersion.Cell.CellWidth + 1) < rightBoarder)
                    {
                        currentCursorX += (MineFieldOldVersion.Cell.CellWidth + 1);
                        playField.CurrentCol++;
                    }
                    HighlightCell(currentCursorX, currentCursorY);
                }
                else if (pressedKey.Key == ConsoleKey.DownArrow)
                {
                    UnHighlightCell(currentCursorX, currentCursorY);
                    if (currentCursorY + 2 * (MineFieldOldVersion.Cell.CellHeight + 1) < bottomBoarder)
                    {
                        currentCursorY += (MineFieldOldVersion.Cell.CellHeight + 1);
                        playField.CurrentRow++;
                    }
                    HighlightCell(currentCursorX, currentCursorY);
                }
                else if (pressedKey.Key == ConsoleKey.UpArrow)
                {
                    UnHighlightCell(currentCursorX, currentCursorY);
                    if (currentCursorY - (MineFieldOldVersion.Cell.CellHeight + 1) >= 0)
                    {
                        currentCursorY -= (MineFieldOldVersion.Cell.CellHeight + 1);
                        playField.CurrentRow--;
                    }
                    HighlightCell(currentCursorX, currentCursorY);
                }
                else if (pressedKey.Key == ConsoleKey.Enter) //Enter for opening blocks
                {
                    OpenCell(currentCursorX, currentCursorY);
                }
                else if (pressedKey.Key == ConsoleKey.Spacebar) //Space for putting flags
                {
                    PlaceFlag(currentCursorX, currentCursorY);
                }
            }
        }
    }
}*/